package com.zyzd.general.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.zyzd.general.R;


/**
 * Created by lzy on 2018/5/16 0016.
 * description ${中等宽度分割线}
 */
public class MiddleSplitView extends View {

    private final Paint mPaint;
    private final int mTopLineHeight;
    private final int mBottomLineHeight;
    private final int mBottomLineColor;
    private int mHeight;
    private int mWidth;
    private int mTopLineColor;

    public MiddleSplitView(Context context) {
        this(context, null);
    }

    public MiddleSplitView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MiddleSplitView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MiddleSplitView, defStyleAttr, 0);
        mTopLineHeight = (int) a.getDimension(R.styleable.MiddleSplitView_topLineHeight, 0);
        mBottomLineHeight = (int) a.getDimension(R.styleable.MiddleSplitView_bottomLineHeight, 0);

        mTopLineColor = a.getColor(R.styleable.MiddleSplitView_topLineColor,getResources().getColor(R.color.color_split));
        mBottomLineColor = a.getColor(R.styleable.MiddleSplitView_bottomLineColor, getResources().getColor(R.color.color_split));

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(mTopLineColor);
        canvas.drawLine(0, 0, mWidth, mTopLineHeight, mPaint);
        mPaint.setColor(mBottomLineColor);
        canvas.drawLine(0, mHeight - mBottomLineHeight, mWidth, mHeight, mPaint);
    }
}
